#include <iostream>
using namespace std;
#define maxn 21
int N, map[maxn][maxn], cost[maxn], path[maxn][maxn], cnt[maxn], total, maxtotal, start;
int bestpath[maxn];
void init()
{
    fill(path[0], path[0] + maxn * maxn, -1);
    fill(cnt, cnt + N + 1, 0);
}
bool isEnd(int now)
{
    for (int i = 1; i <= N; ++i)
    {
        if (map[now][i] == 1)
            return false;
    }
    return true;
}
void dfs(int st, int now)
{
    total += cost[now];
    if (isEnd(now))
    {
        if (maxtotal < total)
        {
            maxtotal = total;
            start = st;
            for (int i = 0; i <= maxn; ++i)
                bestpath[i] = path[start][i];
        }
        return;
    }
    if (cnt[now] > total)
        return;
    else
        cnt[now] = total;
    for (int i = now + 1; i <= N; ++i)
    {
        if (map[now][i] == 1)
        {
            path[st][now] = i;
            dfs(st, i);
            total -= cost[i]; //回溯
        }
    }
}
void showpath(int st)
{
    while (st != -1)
    {
        cout << st << " ";
        st = bestpath[st];
    }
}
int main()
{
    cin >> N;
    init();
    for (int i = 1; i <= N; ++i)
    {
        cin >> cost[i];
    }
    for (int i = 1; i < N; ++i)
    {
        for (int j = i + 1; j <= N; ++j)
        {
            cin >> map[i][j]; //DAG
        }
    }
    for (int i = 1; i <= N; ++i)
        total = 0, dfs(i, i);
    showpath(start);
    cout << endl
         << maxtotal << endl;
    return 0;
}